#include "Parser.h"
#include "Compilation.h"
#include <exception>
#include <cstdlib>
#include <iostream>

using namespace std;
const char *escapedCharacterDFA = "./EscapedCharacter.dfa";

bool runTest(vector<string> files) {

	Compilation c;

	bool parse_failed = false;
	bool type_resolution_failed = false;

	for (vector<string>::iterator it = files.begin(); it < files.end(); ++it) {
		Parser p("./JOOS.pt", it->c_str(), "./Scanner.dfa");
		if (!p.IsAValidProgram()) { parse_failed = true; break; }
		c.Add((CompilationUnitNode*)p.Root());
	}

	if (!parse_failed) c.Compile();
	else type_resolution_failed = true;


	type_resolution_failed = c.error != Compilation::None;
	if (parse_failed || type_resolution_failed) {
		return false;
	}
	return true;
}
int main(int argc, char *argv[]) {



	//this is final release code for gathering files from command line

	vector<string> inputFiles;

	if (argc < 2) {
		cerr << "no input files, exiting" << endl;
	}

	else {
		for (int i = 1; i < argc; i++) {
			inputFiles.push_back(argv[i]);
		}
	}

	bool compiled_successfully = runTest(inputFiles);
	cout << compiled_successfully << endl;
	return (compiled_successfully ? 0 : 42);
}